//初始化xmpp基本参数
	xmpp.init({
		BOSH_SERVICE:"/xmpp-httpbind",
		BOSH_SERVICE_IP:"58.215.190.135"
	});
	//登陆服务器
	var opt={
			name:$("#name").val(),
			password:$("#password").val()
		}	
	var pushBallObj ;
	$("#login").click(function(){
		
		xmpp.login({name:opt.name,password:opt.password,callback:function(user){
			xmpp.joinRoom({room:"pushball",nikname:opt.name});
			pushBallObj = pushBall();
		}});
	
	});
	
	xmpp.listenerHander({
		onMessage:function(msg){
			console.log(msg.to.jid+"------"+msg.body);
			if(msg.from.resource!=opt.name){
				pushBallObj.changeAimHander(msg.body);
			}
			
			
		},
		onIq:function(iq){
			
		},
		onPresence:function(opt){
			console.log(opt.from.jid);
		}
	});
	
function pushBall(){

	var b2Vec2 = Box2D.Common.Math.b2Vec2
	, b2BodyDef = Box2D.Dynamics.b2BodyDef
	, b2Body = Box2D.Dynamics.b2Body
	, b2FixtureDef = Box2D.Dynamics.b2FixtureDef
	, b2World = Box2D.Dynamics.b2World
	, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
	, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
	, b2RevoluteJointDef=Box2D.Dynamics.Joints.b2RevoluteJointDef
	, b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef
	, b2DebugDraw = Box2D.Dynamics.b2DebugDraw
	, b2Fixture = Box2D.Dynamics.b2Fixture
	, b2AABB = Box2D.Collision.b2AABB;
			
     var world = new b2World(new b2Vec2(0, 0), true);
     
     //球
     var fixDef = new b2FixtureDef;
     fixDef.density = 10.0; //定义密度
     fixDef.friction = 0.5; //定义摩擦系数
     fixDef.restitution = 0.5; //定义恢复系数
     fixDef.shape = new b2CircleShape; //定义形状
     fixDef.shape.SetRadius(getPx(20)); //定义尺寸
     
     
     var bodyDef = new b2BodyDef;
     bodyDef.bullet = true;
     bodyDef.type = b2Body.b2_dynamicBody; //定义物体类型
     bodyDef.position.Set(getPx(200), getPx(550)); //定义位置
     bodyDef.userData = "ball";
     var happyBox = world.CreateBody(bodyDef);
     happyBox.SetAngularDamping(0.5);
     happyBox.CreateFixture(fixDef);
     
     //手上握住的球
     fixDef.shape = new b2CircleShape; //定义形状
     fixDef.shape.SetRadius(getPx(25)); //定义尺寸
     fixDef.density = 20.0; //定义密度
     
     var handerBox = new b2BodyDef;
     handerBox.type = b2Body.b2_dynamicBody; //定义物体类型
     handerBox.position.Set(getPx(200), getPx(600)); //定义位置
     handerBox.userData = "hander";
     //固定使其没有转动
     handerBox.fixedRotation = true;
     var handerBoxholder = world.CreateBody(handerBox);
     handerBoxholder.CreateFixture(fixDef);
     
     //对手手上握住的球
     
     
     var aimHanderBox = new b2BodyDef;
     aimHanderBox.type = b2Body.b2_dynamicBody; //定义物体类型
     aimHanderBox.position.Set(getPx(200), getPx(50)); //定义位置
     aimHanderBox.userData = "aimhander";
     //固定使其没有转动
     aimHanderBox.fixedRotation = true;
     var aimHanderBoxholder = world.CreateBody(aimHanderBox);
     aimHanderBoxholder.CreateFixture(fixDef);
     
     //墙 皮夹定义
     var wallFix = new b2FixtureDef;
     wallFix.friction = 0;
     wallFix.restitution = 0.5;
     wallFix.shape = new b2PolygonShape;
     wallFix.shape.SetAsBox(getPx(400), getPx(1));
     
     //墙top-left
     var wallTopLeft = new b2BodyDef;
     wallTopLeft.type = b2Body.b2_staticBody;
     wallTopLeft.position.Set(0, 0);
     wallTopLeft.userData = "top-wall";
     var wallTopholderLeft = world.CreateBody(wallTopLeft)
     	wallTopholderLeft.CreateFixture(wallFix);
     
     //墙bottom
     var wallBottom = new b2BodyDef;
     wallBottom.type = b2Body.b2_staticBody;
     wallBottom.position.Set(0, getPx(650));
     
     var wallBottomholder = world.CreateBody(wallBottom)
     	wallBottomholder.CreateFixture(wallFix);
     
     wallFix.shape.SetAsBox(getPx(1), getPx(650));
     //墙left
     var wallLeft = new b2BodyDef;
     wallLeft.type = b2Body.b2_staticBody;
     wallLeft.position.Set(0, 0);
     
     var wallLeftholder = world.CreateBody(wallLeft)
     	wallLeftholder.CreateFixture(wallFix);
     
     //墙right
     var wallRight = new b2BodyDef;
     wallRight.type = b2Body.b2_staticBody;
     wallRight.position.Set(getPx(400), 0);
     
     var wallRightholder = world.CreateBody(wallRight)
     	wallRightholder.CreateFixture(wallFix);
     
     //door 门
     var doorFix = new b2FixtureDef;
     doorFix.friction = 0;
     doorFix.restitution = 0.5;
     doorFix.shape = new b2PolygonShape;
     doorFix.shape.SetAsBox(getPx(80), getPx(0));
     //对手门
     var doorTop = new b2BodyDef;
     doorTop.type = b2Body.b2_staticBody;
     doorTop.position.Set(getPx(200), getPx(3));
     doorTop.userData = "door";
     var doorTopholder = world.CreateBody(doorTop);
     doorTopholder.CreateFixture(doorFix);
     //自己门
     var doorBottom = new b2BodyDef;
     doorBottom.type = b2Body.b2_staticBody;
     doorBottom.position.Set(getPx(200), getPx(650));
     
     var doorBottomholder = world.CreateBody(doorBottom)
     	doorBottomholder.CreateFixture(doorFix);
     
     var listener = new Box2D.Dynamics.b2ContactListener;
     listener.BeginContact = function (contact) {
     	if (contact.GetFixtureA().GetBody().GetUserData() == "door" && contact.GetFixtureB().GetBody().GetUserData() == "ball") { // 想想为什么不直接用fixture 的 userData。
     		
     		win(contact);
     	}
     	if (contact.GetFixtureA().GetBody().GetUserData() == "hander" && contact.GetFixtureB().GetBody().GetUserData() == "ball") { // 想想为什么不直接用fixture 的 userData。
     		
     		deitl(contact.GetFixtureB().GetBody());
     	}
     }
     listener.EndContact = function (contact) {
     	if (contact.GetFixtureA().GetBody().GetUserData() == "ball" && contact.GetFixtureB().GetBody().GetUserData() == "ball") // 想想为什么不直接用fixture 的 userData。
     		console.log("结束碰撞");
     }
     
     var debugDraw = new b2DebugDraw();
     debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
     debugDraw.SetDrawScale(30); //定义比例
     debugDraw.SetFillAlpha(0.3); //define transparency
     debugDraw.SetLineThickness(1.0);
     debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
     world.SetDebugDraw(debugDraw);
     
     window.setInterval(update, 1000 / 30);
     
     //鼠标
     
     var mouseX, mouseY, mousePVec, isMouseDown, selectedBody, mouseJoint;
     var canvasPosition = getElementPosition(document.getElementById("canvas"));
     
     document.addEventListener("mousedown", function (e) {
     	isMouseDown = true;
     	handleMouseMove(e);
     	document.addEventListener("mousemove", handleMouseMove, true);
     }, true);
     
     document.addEventListener("mouseup", function () {
     	document.removeEventListener("mousemove", handleMouseMove, true);
     	isMouseDown = false;
     	mouseX = undefined;
     	mouseY = undefined;
     }, true);
     
     function handleMouseMove(e) {
     	mouseX = (e.clientX - canvasPosition.x) / 30;
     	mouseY = (e.clientY - canvasPosition.y) / 30;
     };
     
     function getBodyAtMouse() {
     	mousePVec = new b2Vec2(mouseX, mouseY);
     	var aabb = new b2AABB();
     	aabb.lowerBound.Set(mouseX - 0.001, mouseY - 0.001);
     	aabb.upperBound.Set(mouseX + 0.001, mouseY + 0.001);
     	
     	// 遍历寻找重合物体
     	
     	selectedBody = null;
     	world.QueryAABB(getBodyCB, aabb);
     	return selectedBody;
     }
     
     function getBodyCB(fixture) {
     	if (fixture.GetBody().GetType() != b2Body.b2_staticBody) {
     		if (fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), mousePVec)) {
     			selectedBody = fixture.GetBody();
     			return false;
     		}
     	}
     	return true;
     }
     
     function update() {
     	
     	if (isMouseDown && (!mouseJoint)) {
     		var body = getBodyAtMouse();
     		//
     		if (body && body.GetUserData() === "hander") {
     			var position = body.GetWorldCenter();
     			var md = new b2MouseJointDef();
     			md.bodyA = world.GetGroundBody();
     			md.bodyB = body;
     			md.target.Set(position.x, position.y);
     			md.collideConnected = true;
     			md.maxForce = 10000 * 100;
     			md.frequencyHz = 5000;
     			md.dampingRatio = 0;
     			mouseJoint = world.CreateJoint(md);
     			body.SetAwake(true);
     			
     		}
     	}
     	
     	if (mouseJoint) {
     		if (isMouseDown) {
     			mouseJoint.SetTarget(new b2Vec2(mouseX, mouseY));
     		} else {
     			world.DestroyJoint(mouseJoint);
     			mouseJoint = null;
     		}
     	}
     	world.SetContactListener(listener);
     	world.Step(1 / 30, 10, 10);
     	world.DrawDebugData();
     	world.ClearForces();
     };
     
     function getElementPosition(element) {
     	var elem = element,
     	tagname = "",
     	x = 0,
     	y = 0;
     	while ((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined")) {
     		y += elem.offsetTop;
     		x += elem.offsetLeft;
     		tagname = elem.tagName.toUpperCase();
     		
     		if (tagname == "BODY")
     			elem = 0;
     		
     		if (typeof(elem) == "object") {
     			if (typeof(elem.offsetParent) == "object")
     				elem = elem.offsetParent;
     		}
     	}
     	
     	return {
     		x : x,
     		y : y
     	};
     }
     
     function getPx(num) {
     	return num / 30;
     }
     
     function win() {
     	console.log("win");
     }
     //
     function deitl(body) {
     	var k = body.GetLinearVelocityFromLocalPoint(new b2Vec2(0, 0));
		var opt = {};
		opt.body = k.x+","+k.y;
		opt.type = "groupchat";
		opt.to = "pushball@conference.58.215.190.135";
		xmpp.sendMsg(opt);
     
     }
	
	return  {
		changeAimHander :function(str){
			var k = str.split(",");
			happyBox.SetLinearVelocity(new b2Vec2(k[0], k[1]));
		}
	}
	
};


